#include "MediaFile.h"
#include "wels/codec_api.h"
#include "SDLPlayer.h"
#include<SDL\SDL.h>
void Write2YUV (char* yuvdata, unsigned char* pData[3], int iStride[2], int iWidth, int iHeight);
void PlayH264BySDL(const char*name){
     MediaFile*mf=NULL;
     ISVCDecoder*pDecoder;
     unsigned char*dts[3];
     int iRet=0;
     char tmp[256]={0x00,0x00,0x00,0x01,0x00};
     SDecodingParam sDecParam={0};
     SBufferInfo sDstParseInfo={0};
     mf=OpenMediaFile(name);
     if(mf==NULL)return;
     InitVideoThread();
     WelsCreateDecoder(&pDecoder);     
     sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
     (*pDecoder)->Initialize(pDecoder,&sDecParam);
     memcpy(tmp+4,mf->AVCInfo.sps,mf->AVCInfo.spslen);
     iRet=(*pDecoder)->DecodeFrame2(pDecoder,tmp,mf->AVCInfo.spslen+4,dts,&sDstParseInfo);
     memcpy(tmp+4,mf->AVCInfo.pps,mf->AVCInfo.ppslen);
     iRet=(*pDecoder)->DecodeFrame2(pDecoder,tmp,mf->AVCInfo.ppslen+4,dts,&sDstParseInfo);
     while(1){           
       MediaFrame*frame=GetNextMediaFrame(mf);
       char*buff=NULL;
       if(frame==NULL)break;
       buff=(char*)malloc(frame->datalen+4);
       memset(buff,0,frame->datalen+4);
       buff[3]=0x01;
       memcpy(buff+4,frame->data,frame->datalen);
       memset(dts,0,sizeof(dts));
       iRet=(*pDecoder)->DecodeFrame2(pDecoder,buff,frame->datalen+4,dts,&sDstParseInfo);
       if(sDstParseInfo.iBufferStatus==1){
          char*yuvdata=(char*)malloc(sDstParseInfo.UsrData.sSystemBuffer.iWidth*sDstParseInfo.UsrData.sSystemBuffer.iHeight*3/2);
          Write2YUV(yuvdata,dts,
          sDstParseInfo.UsrData.sSystemBuffer.iStride,
          sDstParseInfo.UsrData.sSystemBuffer.iWidth,
          sDstParseInfo.UsrData.sSystemBuffer.iHeight);
          
          ShowOnePic(yuvdata,
          sDstParseInfo.UsrData.sSystemBuffer.iWidth,
          sDstParseInfo.UsrData.sSystemBuffer.iHeight);
          free(yuvdata);
          SDL_Delay(40);
       }
#if 1
       printf("RET:%d iBufferStatus:%d %d \n",iRet,
           sDstParseInfo.iBufferStatus,
           sDstParseInfo.UsrData.sSystemBuffer.iWidth
       );
#endif
       free(buff);
       FreeMediaFrame(frame);
     }
     (*pDecoder)->Uninitialize(pDecoder);
     WelsDestroyDecoder(pDecoder);
     CloseMediaFile(mf);
     ClearVideoThread();
}
void Write2YUV (char*yuvdata, unsigned char* pData[3], int iStride[2], int iWidth, int iHeight) {
    int   i;
    unsigned char*  pPtr = NULL;
    char*Pos=yuvdata;
    //return;
    pPtr = pData[0];
    for (i = 0; i < iHeight; i++) {
        memcpy(Pos,pPtr,iWidth);
        pPtr += iStride[0];
        Pos+=iWidth;
    }

    iHeight = iHeight / 2;
    iWidth = iWidth / 2;
    pPtr = pData[1];
    for (i = 0; i < iHeight; i++) {
        memcpy(Pos,pPtr,iWidth);
        pPtr += iStride[1];
        Pos+=iWidth;
    }
    
    pPtr = pData[2];
    for (i = 0; i < iHeight; i++) {
        memcpy(Pos,pPtr,iWidth);
        pPtr += iStride[1];
        Pos+=iWidth;
    }
}